﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;

namespace Wicresoft.UnifyShow.Portal
{
    public class CustomLoginUtil
    {
        private static readonly string __loginKey = "UnifySiteLogin";
        private static readonly string __encryptKey = "ABCDEFGHIJKLMNOP";

        /// <summary>
        /// log in with out authenticate, set cookie encryptly
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="remember"></param>
        public static void Login(string Name, bool remember)
        {
            if(HttpContext.Current == null)
                throw new Exception("No httpcontext.");

            string raw = CryptoHelper.Encrypt(Name, __encryptKey);
            HttpCookie cookie = new HttpCookie(__loginKey, raw);
            if (remember)
                cookie.Expires.AddYears(100);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetUser()
        {
            if(HttpContext.Current == null)
                throw new Exception("No httpcontext.");

            HttpCookie hCookie = HttpContext.Current.Request.Cookies[__loginKey];
            if (hCookie == null)
                return null;
            return CryptoHelper.Decrypt(hCookie.Value, __encryptKey);
        }
    }
}